//
//  ST_AvatarSupplementView.swift
//  btvideo
//
//  Created by Jason on 2021/11/9.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import NSObject_Rx
import SVGAPlayer

class ST_AvatarSupplementView: UIView {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        [icon_image_view,player].forEach { v in
            
            self.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
        
        self.isUserInteractionEnabled = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
       control.contentMode = UIView.ContentMode.scaleAspectFill
       return control
    }()
    
    fileprivate lazy var player:SVGAPlayer = {
        
        let p  = SVGAPlayer()
        p.loops = 0
        return p
    }()
    
    /// 守护的URL  和 性别
    func bind(gender:Int,url:String?) {
        
        icon_image_view.isHidden = true
        player.isHidden = true
        
        if let urlStr = url{
            
            icon_image_view.pin_setImage(from: urlStr.url)
            icon_image_view.isHidden = false
            player.isHidden = true
            
        }else if let svgaURL = SC.festivalIcon(gender: gender){
            
            ST_GiftAssetProvider.playSVGASession(urlString: svgaURL).withUnretained(self).subscribe { own , entity in
            
                own.player.videoItem = entity
                
                own.player.startAnimation()
                
                own.icon_image_view.isHidden = true
                own.player.isHidden = false
                
            }.disposed(by: rx.disposeBag)
            
        }
        
    }
    
}
